"""blog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path

from django.http import HttpRequest, HttpResponse, JsonResponse

from django.shortcuts import render # 模板快捷方式
from django.template import loader, RequestContext

from django.urls import include # 包含其他url conf

# 实际开发中不要把请求处理方法放到urls.py内，该文件只用于配置url映射
# 实际写到其他文件或者模块中，导入即可
def index(request):
    print(request)
    # return HttpResponse('hello world')
    # return JsonResponse({'my_key':'hello django'}) # json返回需要传递dict
    # return render(request, 'index.html', {'msg':'hello world'})
    # shortcut快捷方式render功能就相当于下面这部分代码，获取模板并渲染
    tpl = loader.get_template('index.html')
    return HttpResponse(tpl.render({'msg':'hello world'}, request))

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', index), # 匹配 /index
    re_path('^index1$', index), # 正则匹配 /index1
    # path('', index), # 匹配根路径
    re_path('^$', index), # 正则，匹配跟路径
    path('user/', include('user.urls')), # 包含其他模块的url配置，多级路由
]
